package com.fr.web.core.service;

import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.list.IntList;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.file.ConfigManager;
import com.fr.report.ArrayPageSet;
import com.fr.report.PaperSetting;
import com.fr.report.PaperSize;
import com.fr.report.ReportPage;
import com.fr.report.ResultWorkBook;
import com.fr.report.io.SWFExporter;
import com.fr.util.Consts;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FlashPrintService extends NoSessionIDService
{
  private static boolean flashPrintable = true;
  private static FlashPrintService FLASHPRINT_SERVICE;

  public static FlashPrintService getInstance()
  {
    return FLASHPRINT_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return ((paramString.startsWith("flash_")) || (paramString.equals("png_print")));
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString.equals("flash_getflashprintsetting"))
      dealWithGetFlashPrintSetting(paramHttpServletResponse);
    else if (paramString.equals("flash_getflashinstallpath"))
      dealWithGetFlashInstallPath(paramHttpServletRequest, paramHttpServletResponse);
    else if (paramString.equals("flash_getpageinfo"))
      dealWithGetPageInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if (paramString.equals("flash_print"))
      dealWithPaintReportPage(paramHttpServletRequest, paramHttpServletResponse, true);
    else if (paramString.equals("png_print"))
      dealWithPaintReportPage(paramHttpServletRequest, paramHttpServletResponse, false);
  }

  public static void dealWithPaintReportPage(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, boolean paramBoolean)
    throws Exception
  {
    if (!(flashPrintable))
      return;
    paramHttpServletResponse.setHeader("Cache-Control", "private");
    paramHttpServletResponse.setHeader("Cache-Control", "max-age=1");
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "sessionID");
    int i = 0;
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pn");
    if (str2 != null)
      i = Integer.parseInt(str2);
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(str1);
    if (localSessionIDInfor == null)
    {
      resFlashDone(paramHttpServletResponse);
      return;
    }
    ReportPage localReportPage = localSessionIDInfor.getPrintPreviewReportPage(i);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    if (paramBoolean)
    {
      paramHttpServletResponse.setContentType("application/x-shockwave-flash");
      new SWFExporter().export(localServletOutputStream, new ArrayPageSet(new ReportPage[] { localReportPage }, false));
    }
    else
    {
      paramHttpServletResponse.setContentType("image/png");
      BaseUtils.writeImage(localReportPage.toImage(), "png", localServletOutputStream);
    }
    localServletOutputStream.flush();
    try
    {
      localServletOutputStream.close();
    }
    catch (IOException localIOException)
    {
    }
  }

  public static void dealWithGetPageInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int j;
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    if (!(flashPrintable))
    {
      localPrintWriter.write("failpass");
      localPrintWriter.flush();
      localPrintWriter.close();
      return;
    }
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "sessionID");
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(str);
    StringBuffer localStringBuffer = new StringBuffer();
    int i = localSessionIDInfor.getPrintPageCount();
    localStringBuffer.append(i);
    IntList localIntList = localSessionIDInfor.getPrintPageSetChainPageInfo();
    if ((localIntList != null) && (localIntList.size() == localSessionIDInfor.getWorkBook2Show().getReportCount()))
      for (j = 0; j < localIntList.size(); ++j)
      {
        int k;
        int l;
        PaperSetting localPaperSetting = localSessionIDInfor.getPagerSetting(j);
        if (localPaperSetting == null)
          localPaperSetting = new PaperSetting();
        PaperSize localPaperSize = localPaperSetting.getPaperSize();
        if (localPaperSetting.getOrientation() == 1)
        {
          k = (int)(localPaperSize.getHeight() * 72.0D);
          l = (int)(localPaperSize.getWidth() * 72.0D);
        }
        else
        {
          k = (int)(localPaperSize.getWidth() * 72.0D);
          l = (int)(localPaperSize.getHeight() * 72.0D);
        }
        if (j == 0)
          localStringBuffer.append(";");
        else
          localStringBuffer.append("&");
        localStringBuffer.append(localIntList.get(j)).append(":").append(k).append(",").append(l);
      }
    localPrintWriter.write(localStringBuffer.toString());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void resFlashDone(HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    paramHttpServletResponse.setContentType("text/plain");
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print("done");
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static void dealWithGetFlashPrintSetting(HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.write(FRContext.getConfigManager().isPopupFlashPrintSetting() + "");
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static void dealWithGetFlashInstallPath(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.write("");
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  static
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      flashPrintable = (localJSONObject.getLong("deadline") > Calendar.getInstance().getTimeInMillis()) && (localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (((localJSONObject.getLong("deadline") - Calendar.getInstance().getTimeInMillis() < 3153600000L) || ((localJSONObject.has("macaddress")) && (BaseCoreUtils.getMacAddresses().contains(localJSONObject.getString("macaddress")))))) && (VT4FR.FLASH_PRINT.support());
    }
    catch (Exception localException2)
    {
    }
    FLASHPRINT_SERVICE = new FlashPrintService();
  }
}